package com.cwk.yygh.hosp.controller.api;


import com.cwk.yygh.common.exception.YyghException;
import com.cwk.yygh.common.helper.HttpRequestHelper;
import com.cwk.yygh.common.result.Result;
import com.cwk.yygh.common.result.ResultCodeEnum;
import com.cwk.yygh.common.utils.MD5;
import com.cwk.yygh.hosp.service.DepartmentService;
import com.cwk.yygh.hosp.service.HospitalService;
import com.cwk.yygh.hosp.service.HospitalSetService;
import com.cwk.yygh.hosp.service.ScheduleService;
import com.cwk.yygh.model.hosp.Department;
import com.cwk.yygh.model.hosp.Schedule;
import com.cwk.yygh.vo.hosp.DepartmentQueryVo;
import com.cwk.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;


    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMds= MD5.encrypt(signKey);
        if(!signKeyMds.equals(sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return Result.ok(hospitalService.getByHoscode(hoscode));
    }
    @PostMapping("saveHospital")
    public Result save(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMds= MD5.encrypt(signKey);

        if(!signKeyMds.equals(sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String logoData=(String)paramMap.get("logoData");
        logoData=logoData.replaceAll(" ", "+");
        paramMap.put("logoData",logoData);
        hospitalService.save(paramMap);
        return Result.ok();
    }
    @PostMapping("department/list")
    public Result getDepartment(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        String hoscode = (String) paramMap.get("hoscode");

        //当前页和每页数
        Integer page= StringUtils.isEmpty(paramMap.get("page"))?1: Integer.parseInt((String)paramMap.get("page"));
        Integer limit= StringUtils.isEmpty(paramMap.get("limit"))?1: Integer.parseInt((String)paramMap.get("limit"));
        //签名判断
//        String sign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMds= MD5.encrypt(signKey);
//        if(!signKeyMds.equals(sign)){
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel=departmentService.findDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMds= MD5.encrypt(signKey);
        if(!signKeyMds.equals(sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //签名判断
//        String sign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMds= MD5.encrypt(signKey);
//        if(!signKeyMds.equals(sign)){
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        departmentService.removeDepartment(depcode,hoscode);
        return Result.ok();
    }

    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        String hoscode = (String) paramMap.get("hoscode");
        //签名判断
//        String sign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMds= MD5.encrypt(signKey);
//        if(!signKeyMds.equals(sign)){
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @PostMapping("schedule/list")
    public Result getSchedule(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //当前页和每页数
        Integer page= StringUtils.isEmpty(paramMap.get("page"))?1: Integer.parseInt((String)paramMap.get("page"));
        Integer limit= StringUtils.isEmpty(paramMap.get("limit"))?1: Integer.parseInt((String)paramMap.get("limit"));
        //签名判断
//        String sign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMds= MD5.encrypt(signKey);
//        if(!signKeyMds.equals(sign)){
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setHoscode(depcode);
        Page<Schedule> pageModel=scheduleService.findSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> map=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //签名判断
//        String sign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMds= MD5.encrypt(signKey);
//        if(!signKeyMds.equals(sign)){
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        scheduleService.removeSchedule(hosScheduleId,hoscode);
        return Result.ok();
    }
}
